package me.lc.linklink.util;

import me.lc.linklink.MainActivity;
import me.lc.linklink.core.StageManager;
import me.lc.linklink.scene.GameScene;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;

import com.nd.dianjin.DianJinPlatform;
import com.nd.dianjin.DianJinPlatform.Oriention;
import com.nd.dianjin.listener.AppActivatedListener;
import com.nd.dianjin.webservice.WebServiceListener;

public class DianJinTool {

	public static String TAG = "DianJoyTool";

	public static void init() {
		// 点金
		DianJinPlatform.initialize(MainActivity.instance, 7246,
				"18df7003c7ce0f0ebef2bb5d06e2cfeb");

		DianJinPlatform.setAppActivatedListener(new AppActivatedListener() {
			@Override
			public void onAppActivatedResponse(int responseCode, Float money) {
				switch (responseCode) {
				case DianJinPlatform.APP_ACTIVATED_SUCESS:
					Log.i(TAG, "奖励M币:" + String.valueOf(money));
					consume(money);
					// 更新现有的金币数量
					StageManager.instance.goldCount += money.intValue();
					StageManager.instance.saveGold();
					GameScene.make().updateGold();
					break;
				case DianJinPlatform.APP_ACTIVATED_ERROR:
					Log.i(TAG, "奖励M币:ERROR");
					break;
				default:
					Log.i(TAG, "奖励M币:ERROR");
					break;
				}
			}
		});
	}

	public static void showOfferWall(final String comfirmTip) {
		MainActivity.instance.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				if (comfirmTip != null) {
					AlertDialog alertDialog = new AlertDialog.Builder(
							MainActivity.instance)
							.setTitle("提示")
							.setMessage(comfirmTip)
							.setPositiveButton("免费赚取",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int which) {
											DianJinPlatform.showOfferWall(
													MainActivity.instance,
													Oriention.PORTRAIT);
										}
									})
							.setNegativeButton("取消",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int which) {
											return;
										}
									}).setCancelable(false).create();
					alertDialog.show();
				} else {
					DianJinPlatform.showOfferWall(MainActivity.instance,
							Oriention.PORTRAIT);
				}
			}
		});
	}

	public static void destroy() {
		DianJinPlatform.destroy();
	}

	private static void consume(final float value) {
		MainActivity.instance.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				DianJinPlatform.consume(MainActivity.instance, value,
						new WebServiceListener<Integer>() {
							@Override
							public void onResponse(int responseCode, Integer t) {
								switch (responseCode) {
								case DianJinPlatform.DIANJIN_SUCCESS:
									Log.i(TAG, "消费" + value);
									break;
								default:
									Log.i(TAG, "消费失败");
									break;
								}
							}
						});

			}
		});

	}
}
